// components/exChange/index.js
import {
  ApiinCompleteOrder
} from "../../../api/docIndex.js"
const app = getApp()
Component({
  /*   patientArr */
  /**
   * 页面的初始数据
   */
  properties: {
    dataVal: {
      type: Object,
    },
    showpopup: {
      type: Boolean
    },
    patientArr: {
      type: Array
    }
  },
  data: {
    name: '所有人',
    active: "all",
    order_type: '1',
    $path: app.globalData.$path,
    param: {
      page: 1,
      size: 10,
      finish: false,
      order_type: '图文问诊'
    }
  },
  pageLifetimes: {
    show() {
      app.ws.onList(true, this.getInProgressData.bind(this))
      /*  this.setData({
         showpopup: this.data.showpopup,
       })
       let useData = this.data.patientArr.findIndex(item => {
         return item.use == '1'
       })
       console.log('useDatauseDatauseData',useData)
       this.setData({
         active: useData,
       }) */
    }
  },




  methods: {
    getInProgressData() {
      let val = wx.getStorageSync('webData')
      const index = this.findLeftDataIndex(val.consultation_order_id)
      if (index > 0) {
        this.data.dataVal.data[0].unshift(this.data.dataVal.data[0].splice(index, 1)[0])
      }
      this.data.dataVal.data.forEach(item => {
        //判断是否有新消息提示
        item.forEach(i => {
          if (i.id == val.consultation_order_id) {
            i.message.last_message = val.content
            i.message.type = val.type
            i.message.doctor_notice++
            this.setData({
              is_patient_notice: true
            })
          }
        })
      })
      this.setData({
        'dataVal.data': this.data.dataVal.data
      })

    },


    findLeftDataIndex(webId) {
      console.log(this.data.dataVal, webId)
      return this.data.dataVal.data[0].findIndex(item => item.id === webId)
    },

    onChange(e) {
      this.setData({
        order_type: e.detail.name
      })
      if (e.detail.name == '2') {
        this.getCompleteData()
      }

    },

    onLoadMore() {
      if (this.data.param.finish) return
      this.data.param.page += 1
      this.getCompleteData()
    },


    getCompleteData() {
      wx.showLoading({
        title: '加载中'
      })
      //保存上一次的page
      let completeData
      ApiinCompleteOrder(this.data.param).then(res => {
        const data = res.data
        completeData = this.data.param.page === 1 ? data : this.data.completeData.concat(data)
        if (data.length == 0) {
          this.data.param.finish = true
        }
        this.setData({
          completeData,
        })
        wx.hideLoading()
      })
    },

    // 切换就诊人
    exChangeClick() {
      this.setData({
        showpopup: true,
      })
    },

    messageClick(e) {
      console.log(e)
      const item = e.currentTarget.dataset.v
      const type = e.currentTarget.dataset.type
      wx.setStorageSync('comment', item.comment)
      const ops = {
        icon: item.icon,
        id: item.id,
        doctor_id: item.message.patient_id,
        doctor_name: item?.patient?.name,
        department_name: item.department_name,
        consultation_order_id: item.message.consultation_order_id,
        comment: item.comment,
        statusS: item.statusS,
        order_time: item.order_time,
        status: type ? '3' : item.status,
        finish_time: item.finish_time,
        cancel_time: item.cancel_time,
        uid: item.uid,
        patient_id: item.patient_id
      }

      wx.setStorageSync('messageDetail', ops)
      wx.navigateTo({
        url: `/pagesDoctor/messageDetail/index?contact=${JSON.stringify(ops)}`,
      })
    },


  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})